﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Utilities
{
    public class Logger
    {
        private const string k_File = "LogFileSC.txt";
        private TextWriter m_Writer;

        private static volatile Logger s_Instance = null;
        private static object s_LockObj = new Object();

        public static Logger Instance
        {
            get
            {
                if (s_Instance == null)
                {
                    lock (s_LockObj)
                    {
                        if (s_Instance == null)
                        {
                            s_Instance = new Logger();
                        }
                    }
                }

                return s_Instance;
            }
        }

        private Logger()
        {
            m_Writer = new StreamWriter(k_File);
        }

        public void WriteToLog(string message)
        {
            StackTrace st = new StackTrace(true);
            StackFrame sf = st.GetFrame(1);

            string debugInfo = string.Format("{0} File: {1}  Method: {2}  Line: {3}", 
                DateTime.Now, parseFileNameFromFullPath(sf.GetFileName()), sf.GetMethod().Name, sf.GetFileLineNumber());
            m_Writer.WriteLine("[{0}]: {1}", debugInfo, message);
        }

        private string parseFileNameFromFullPath(string fileFullPath)
        {
            string[] parts = fileFullPath.Split('\\');
            return parts[parts.Length - 1];
        }

        public void CloseLogger()
        {
            m_Writer.Close();
        }
    }
}
